{
  "name": "Budget Deviation & Overspend Approval Agent",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"]
      },
      "id": "tg-trigger-budget",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "user_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "name": "user_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "set-budget-context",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Budget Deviation and Overspend Approval Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new budget deviation request or CHECK deviation status.\n\n2. For new deviation requests, collect:\n   - Cost Center Code\n   - Budget Period (Month / Quarter)\n   - Approved Budget Amount\n   - Actual Spend Amount\n   - Reason for Overspend\n\n3. Validate the cost center using the Cost Center Budget Master Tool and extract:\n   - Cost_Center_Name\n   - Owner_Email (used for email communication)\n   - Budget_Status\n\n4. If cost center is valid and active:\n   - Generate a deviation reference ID in format:\n     BDG-YYYYMMDD-[last 4 digits of cost center]-HHmmss\n   - Register the deviation using the Budget Deviation Register Tool\n   - Send acknowledgement email using Gmail Tool to Owner_Email\n   - Explain approval workflow and expected decision timeline\n   - Respond using Telegram Response Tool\n\n5. If cost center is invalid or inactive:\n   - Politely explain the reason\n   - Do NOT register the deviation\n   - Respond using Telegram Response Tool\n\n6. For status checks:\n   - Ask for deviation reference ID if missing\n   - Retrieve status using the Budget Deviation Status Tool\n   - Explain current approval stage and next steps\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT promise approval.\n- Do NOT provide financial advice.\n- Email must be retrieved from master sheet only.\n- ALWAYS respond using Telegram Response Tool."
        }
      },
      "id": "agent-budget",
      "name": "Budget Deviation Approval Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-budget",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "master-budget",
      "name": "Cost Center Budget Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 240]
    },
    {
      "parameters": {
        "operation": "append",
        "descriptionType": "manual"
      },
      "id": "register-budget",
      "name": "Budget Deviation Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 240]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "status-budget",
      "name": "Budget Deviation Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 300]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('owner_email') }}",
        "subject": "Budget Deviation Request Submitted",
        "message": "Your budget deviation request has been registered and is under review."
      },
      "id": "gmail-budget",
      "name": "Budget Deviation Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-budget",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Budget Deviation Approval Agent", "type": "main" }]]
    },
    "Budget Deviation Approval Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
